import { useSetState } from "ahooks";
import React, { useState } from "react";
import { MOCK_LIST } from "./store";

function usePlugInSheet() {
  const [visible, setVisible] = useState(false);
  const [plugInState, setPlugInState] = useSetState<IPlugInState>({
    checkedList: [],
    list: MOCK_LIST,
  });

  const handleSwitchChange = (value: boolean, item: IPlugInItem) => {
    const cList = plugInState.checkedList ?? [];
    if (value) {
      cList.push(item.id);
    } else {
      const index = cList.indexOf(item.id);
      cList.splice(index, 1);
    }
    setPlugInState({
      checkedList: [...cList],
    });
  };

  return {
    visible,
    plugInState,
    setVisible,
    handleSwitchChange,
  };
}

export default usePlugInSheet;
